package ${t.packageName}.service;

import com.oleasy.platform.api.common.exception.ServiceException;
import com.oleasy.platform.api.common.model.${t.entityName}Entity;
import com.oleasy.platform.api.common.model.Q${t.entityName}Entity;
import ${t.packageName}.repository.${t.entityName}Repository;
import com.oleasy.platform.api.common.repository.jpa.base.BaseRepository;
import com.oleasy.platform.api.common.restapi.Pager;
import com.oleasy.platform.api.common.restapi.ResultCode;
import com.oleasy.platform.api.common.service.base.impl.BaseServiceImpl;
import com.oleasy.platform.api.common.utils.BeanUtils;
import com.oleasy.platform.api.common.utils.StringUtils;
import ${t.packageName}.model.entity.${t.entityName};
import ${t.packageName}.model.request.${t.entityName}Query;
import com.querydsl.core.types.Predicate;
import io.jsonwebtoken.lang.Collections;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("${t.lowEntityName}Service")
public class ${t.entityName}ServiceImpl extends BaseServiceImpl<${t.entityName}Entity> implements ${t.entityName}Service {

    @Resource
    private ${t.entityName}Repository ${t.lowEntityName}Repository;

    @Override
    protected BaseRepository<${t.entityName}Entity> getRepository() {
        return ${t.lowEntityName}Repository;
    }

    @Override
    public Page<${t.entityName}> find(${t.entityName}Query ${t.lowEntityName}Query) {
        Q${t.entityName}Entity q${t.entityName}Entity = Q${t.entityName}Entity.${t.lowEntityName}Entity;
        Predicate predicate = q${t.entityName}Entity.isNotNull().or(q${t.entityName}Entity.isNull());
        return findAll(predicate, Pager.of(${t.lowEntityName}Query, getDefaultSort())).map(${t.lowEntityName}Entity -> BeanUtils.copyProperties(${t.lowEntityName}Entity, ${t.entityName}.
        class));
    }

    @Override
    public boolean delete(List<String> idList) {
        if (Collections.isEmpty(idList)) {
            throw new ServiceException(ResultCode.ID_IS_NULL);
        }
        idList.forEach(id -> {
            if (!deleteById(id)) {
                throw new ServiceException(ResultCode.DELETE_FAILED);
            }
        });
        return true;
    }

    @Override
    public ${t.entityName} save(${t.entityName} ${t.lowEntityName}) {
        if (${t.lowEntityName} ==null){
            throw new ServiceException(ResultCode.INVALID);
        }
        ${t.entityName}Entity ${t.lowEntityName}Entity = null;
        if (StringUtils.isNotBlank(${t.lowEntityName}.getId())){
            ${t.lowEntityName}Entity = findById(${t.lowEntityName}.getId()).
            orElseThrow(() -> new ServiceException(ResultCode.NOT_FOUND));
        } else{
            ${t.lowEntityName}Entity = new ${t.entityName}Entity();
        }
        BeanUtils.copyProperties(${t.lowEntityName}, ${t.lowEntityName}Entity);
        return BeanUtils.copyProperties(this.save(${t.lowEntityName}Entity), ${t.entityName}. class);
    }

}
